const Router = require('koa-router');
const wxUser = new Router();
const bodyparser = require('koa-bodyparser');


//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
    database: 'running',
    user: 'run',
    port: 5432,
    host: '82.157.94.115',
    password: 'z12345678',
    max: 5
  })

const jwt = require('jsonwebtoken');
//调用中间件，获取小程序post传过来的数据
wxUser.post('/wxregister', async (ctx, next) => {
    console.log('wxregister');
    let { userNick, avatarUrl } = ctx.request.body;
    function makeId() {
        let id = Math.random().toString(16).substring(2);
        let id2 = Math.random().toString(16).substring(2);
        return (id+id2).substring(0, 12);
      }
      let insertPostgresql = `insert into login (openid,userNick, avatarUrl) values ($1,'${userNick}', '${avatarUrl}')`
      let result = psql.query(insertPostgresql, [makeId()]);
        if (result.rowCount > 0)
            ctx.body = {
                msg: "授权成功",
                userNick: userNick,
                avatarUrl:avatarUrl
            };
});
module.exports = wxUser;

